//
//  ProductData.m
//  BosidengIphone
//
//  Created by  on 12-1-7.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "ProductData.h"
#import "AppDelegate.h"

@implementation ProductData
@synthesize productID;
@synthesize brandID;
@synthesize name;
@synthesize iPhoneImages;
@synthesize introduction;
@synthesize item;
@synthesize color;
@synthesize fabric;
@synthesize isRecommend;
@synthesize views;
@synthesize shopUrl;
@synthesize modifyDate;

- (id)init {
    if (self = [super init]) {
        ;
    }
    return self;
}

- (void)dealloc {
    [productID release];
    [brandID release];
    [name release];
    [iPhoneImages release];
    [introduction release];
    [item release];
    [color release];
    [fabric release];
    [isRecommend release];
    [views release];
    [shopUrl release];
    [modifyDate release];
    [super dealloc];
}

- (void)setValue:(NSDictionary *)data {
    [super setValue:data];
    self.productID = [data objectForKey:@"ProductID"];
    self.brandID = [data objectForKey:@"BrandID"];
    self.name = [data objectForKey:@"Name"];
    self.iPhoneImages = [self formatStringData:[data objectForKey:@"iPhoneImages"]];
    self.introduction = [data objectForKey:@"Introduction"];
    self.item = [data objectForKey:@"Item"];
    self.color = [data objectForKey:@"Color"];
    self.fabric = [data objectForKey:@"Fabric"];
    self.isRecommend = [data objectForKey:@"IsRecommend"];
    self.views = [data objectForKey:@"Views"];
    self.shopUrl = [data objectForKey:@"ShopUrl"];
    self.modifyDate = [data objectForKey:@"ModifyDate"];
}

- (BOOL)hasImgs {
    return ((iPhoneImages != nil) && ![@" " isEqualToString:iPhoneImages]);
}

- (void)removeAllImages {
    NSArray *images = [iPhoneImages componentsSeparatedByString: @"|"];
    for (NSString *imgInfo in images) {
        NSArray *infos = [imgInfo componentsSeparatedByString:@","];
        NSString *imagePath = [AppDelegate getCacheFilePathByUrl:[infos objectAtIndex:0]];
        [AppDelegate deleteFile:imagePath];
    }
}

- (NSString *)getHeadImageUrl {
    if (![self hasImgs]) {
        return @"";
    }
    NSArray *images = [iPhoneImages componentsSeparatedByString: @"|"];
    NSString *imgInfo = [images objectAtIndex:0];
    NSArray *infos = [imgInfo componentsSeparatedByString:@","];
    return [infos objectAtIndex:0];
}

- (UIFont *)getFont {
    return [UIFont systemFontOfSize:9];
}

- (NSInteger)getContentHeight {
    return [self getTextHeight:introduction cx:300 font:[self getFont]];
}

- (NSInteger)getLineNumbers {
    CGFloat height = [self getContentHeight];
    CGFloat lineHeight = [self getFont].lineHeight;
	return height / lineHeight;
}

+ (NSString *)getAllItemsQueryString {
    NSString *query = [NSString stringWithFormat:@"WHERE language='%@' ORDER BY modify_date DESC", [AppDelegate getLanguage]];
    return query;
}

+ (id)getItemByID:(NSString *)uid {
    return [ProductData findFirstByCriteria:[ProductData getItemQueryString:@"product_i_d" value:uid]];
}

+ (NSArray *)getAllItems {
    return [ProductData findByCriteria:[ProductData getAllItemsQueryString]];
}

+ (NSArray *)getItemsByBrandId:(NSString *)brandId {
    return [ProductData findByCriteria:[NSString stringWithFormat:@"WHERE language='%@' AND brand_i_d='%@' ORDER BY modify_date DESC", [AppDelegate getLanguage], brandId]];
}

+ (NSArray *)getRecommendItems {
    return [ProductData findByCriteria:[NSString stringWithFormat:@"WHERE language='%@' AND is_recommend='true' ORDER BY modify_date DESC", [AppDelegate getLanguage]]];
}

- (NSString *)description {
    return [NSString stringWithFormat:@"News Item.%d, productID=%@", [self pk], productID];
}

@end
